//
//  AddressEditCell.m
//  Ufan
//
//  Created by yangqingluo on 15/12/9.
//  Copyright © 2015年 yangqingluo. All rights reserved.
//

#import "AddressEditCell.h"
#import "XHMessageTextView.h"

@interface AddressEditCell()<UITextViewDelegate>

@property (strong, nonatomic) XHMessageTextView *textView;
@property (strong, nonatomic) UIView *bottomLineView1;

@end

@implementation AddressEditCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.textView = [[XHMessageTextView alloc]init];
        self.textView.showsVerticalScrollIndicator = NO;
        self.textView.font = [UIFont systemFontOfSize:16.0];
        self.textView.delegate = self;
        [self.contentView addSubview:self.textView];
        
        _bottomLineView1 = [[UIView alloc] init];
        _bottomLineView1.backgroundColor = UFCOLOR_GRAYLINE;
        [self.contentView addSubview:_bottomLineView1];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    _bottomLineView1.frame = CGRectMake(15, self.height - 1, self.width - 30, 1);
    self.textView.frame = CGRectMake(0, 0, _bottomLineView1.width, (self.type == AddressEditCellTypeDetailAddress ? 0.9 : 0.6) * self.height);
    self.textView.center = CGPointMake(0.5 * self.width, 0.5 * self.height);
    
    switch (self.type) {
        case AddressEditCellTypeDeliveryName:{
            self.textView.placeHolder = @"收货人姓名";
            self.textView.text = self.address.deliveryName;
        }
            break;
            
        case AddressEditCellTypeDeliveryPhone:{
            self.textView.placeHolder = @"收货人手机号";
            self.textView.keyboardType = UIKeyboardTypePhonePad;
            self.textView.text = self.address.deliveryPhone;
        }
            break;
            
        case AddressEditCellTypeDetailAddress:{
            self.textView.placeHolder = @"详细地址";
            self.textView.text = self.address.detailAddress;
        }
            break;
            
        case AddressEditCellTypeName:{
            self.textView.placeHolder = @"备注(给地址取个名吧，比如家，公司)";
            self.textView.text = self.address.name;
        }
            break;
            
        default:
            break;
    }
    
    
}


+(CGFloat)tableView:(UITableView *)tableView heightForType:(AddressEditCellType)type{
    switch (type) {
        case AddressEditCellTypeDetailAddress:{
            return 100;
        }
            break;
            
        default:{
            return 50;
        }
            break;
    }
}

#pragma mark---UITextViewDelegate
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{

    
    return YES;
}
//即时观察textView的变化
- (void)textViewDidChange:(UITextView *)textView{
//    //限制200字以内,该判断用于联想输入
//    if (textView.text.length > 200){
//        textView.text = [textView.text substringToIndex:200];
//    }
    
    switch (self.type) {
        case AddressEditCellTypeDeliveryName:{
            self.address.deliveryName = textView.text;
        }
            break;
            
        case AddressEditCellTypeDeliveryPhone:{
            self.address.deliveryPhone = textView.text;
        }
            break;
            
        case AddressEditCellTypeDetailAddress:{
            self.address.detailAddress = textView.text;
        }
            break;
            
        case AddressEditCellTypeName:{
            self.address.name = textView.text;
        }
            break;
            
        default:{
            
        }
            break;
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    switch (self.type) {
        case AddressEditCellTypeDeliveryName:{
            return (range.location < kNameLength);
        }
            break;
            
        case AddressEditCellTypeDeliveryPhone:{
            return (range.location < kPhoneNumberLength);
        }
            break;
            
        case AddressEditCellTypeDetailAddress:{
            return (range.location < kAddressLength);
        }
            break;
            
        case AddressEditCellTypeName:{
            return (range.location < kNameLength);
        }
            break;
            
        default:{
            return (range.location < kNameLength);
        }
            break;
    }
}

@end
